package com.example.springboot.service.admin.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.config.Shiro.security.SecurityUtil;
import com.example.springboot.mapper.EnshrineMapper;
import com.example.springboot.mapper.MaterialMapper;
import com.example.springboot.mapper.WallHavenMapper;
import com.example.springboot.model.entity.Enshrine;
import com.example.springboot.model.entity.Material;
import com.example.springboot.model.entity.WallHaven;
import com.example.springboot.model.form.PageUtil;
import com.example.springboot.model.form.Result;
import com.example.springboot.model.req.admin.WallHavenReq;
import com.example.springboot.model.sys.SysUser;
import com.example.springboot.model.vo.WallpaperVO;
import com.example.springboot.service.admin.WallHavenService;
import com.example.springboot.utils.CopyUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author zf
 * @
 * @since 2021-04-16
 */
@Service
public class WallHavenServiceImpl extends ServiceImpl<WallHavenMapper, WallHaven> implements WallHavenService {

    @Resource
    MaterialMapper materialMapper;

    @Resource
    EnshrineMapper enshrineMapper;
    @Override
    public Result getPage(WallHavenReq req) {

        SysUser sysUser = SecurityUtil.sysUser();
        List<Integer> ids = null;
        if (Objects.nonNull(sysUser)) {
            List<Enshrine> enshrines = enshrineMapper.selectList(new LambdaQueryWrapper<Enshrine>()
                    .eq(Enshrine::getInvalid, 0)
                    .eq(Enshrine::getType, 2)
                    .eq(Enshrine::getUserId, sysUser.getId()));
            ids = enshrines.stream().map(e -> e.getEnshrineId()).collect(Collectors.toList());
            ids.add(-1);
        }

        IPage<WallHaven> ipage = page(req.toPage(), new LambdaQueryWrapper<WallHaven>()
                .eq(WallHaven::getInvalid, 0)
                .in(Objects.nonNull(ids),WallHaven::getId,ids)
                .orderByDesc(WallHaven::getCreateTime));

        Page<WallHaven> page = PageUtil.toPage(ipage);
        return Result.buildSuccess(page.getRecords().stream().map(e -> {
            WallpaperVO vo = CopyUtil.copyObject(e, WallpaperVO.class);

            //随机素材
            Material material = materialMapper.selectOne(new LambdaQueryWrapper<Material>()
                    .eq(Material::getInvalid, 0)
                    .orderByDesc(Material::getId)
                    .last("limit 1"));
            while (true) {
                int num = new Random().nextInt(material.getId() + 1);
                Material getMaterial = materialMapper.selectById(num);
                if (getMaterial != null) {
                    vo.setContentId(getMaterial.getId());
                    vo.setContent(getMaterial.getContent());
                    vo.setSource(getMaterial.getSource());
                    vo.setAuthor(getMaterial.getAuthor());
                    break;
                }
            }
            return vo;
        }));
    }

    @Override
    public Result addInfo(WallHavenReq req) {
        //主表复制元素
        WallHaven wallHaven = CopyUtil.copyObject(req, WallHaven.class);
        boolean flag;
        if (wallHaven.getId() != null) {
            flag = updateById(wallHaven);
        } else {
            flag = save(wallHaven);
        }
        if (flag) {
            return Result.buildSuccess();
        }
        return Result.buildFail();
    }

    @Override
    public Result getInfo(Integer id) {
        return Result.buildSuccess(getById(id));
    }

    @Override
    public Result deleteInfos(String ids) {
        if (update(new UpdateWrapper<WallHaven>().lambda()
                .set(WallHaven::getInvalid, 1)
                .inSql(WallHaven::getId, ids)
        )) {
            return Result.buildSuccess();
        }
        return Result.buildFail();
    }

}
